export default {
	data() {
		return {
			permissionCameraContent: "获取拍照权限是为了发送聊天图片",
				permissionAlbumContent: "获取相册权限，是为了发送聊天图片",
				permissionCameraContent1: "获取拍照权限是为了发送聊天视频",
				permissionAlbumContent1: "获取相册权限，是为了发送聊天视频",
				permissionContent: '',
				content: '',
				content1: '发送图片需要打开相机的权限，请到应用设置里面开启。',
				content2: '发送图片需要打开相册的权限，请到应用设置里面开启。',
				content3: '录像需要打开相机的权限，请到应用设置里面开启。',
				content4: '发送视频需要打开相册的权限，请到应用设置里面开启。',
				uploadImg: '',
		}
	},
	mounted() {
		// #ifdef APP-PLUS
		// #endif
	},
	methods: {
		toOpenAppPermission() {
      this.$refs.permissionModel.close()
    },
    handleSendPicture() {
      this.$refs.uploadImg.openAppPermissionAndUploadCameraImage()
      // #ifdef APP-PLUS
      this.androidCameraPermission('camera', this.content1, this.permissionCameraContent)
      // #endif
      // this.sendImageMessage('camera');
    },
    handleSendImage() {
      // openAppPermissionAndUploadAlbum
      this.$refs.uploadImg.openAppPermissionAndUploadAlbumImage()
      // #ifdef APP-PLUS
      this.androidCameraPermission('album', this.content2, this.permissionAlbumContent)
      // #endif
      // this.sendImageMessage('album');
    },
    androidCameraPermission(type, content, permissionContent) {
      let options = {
        'camera': 'android.permission.CAMERA',
        'album': 'android.permission.WRITE_EXTERNAL_STORAGE'
      }
      let permission = options[type]
      // let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
      var Manifest = plus.android.importClass("android.Manifest");
      var MainActivity = plus.android.runtimeMainActivity();
      let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
      if (result) {
        plus.android.requestPermissions([permission], e => {
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            this.content = content
            this.$refs.model.open()
            this.$refs.permissionModel.close()
          } else if (e.deniedPresent.length > 0) {
            this.$refs.permissionModel.close()
          } else if (e.granted.length > 0) {
            this.$refs.permissionModel.close(); // 别忘了关闭提示
            if (type == 'camera') {
              // this.uploadCamera(); // 同意则调用方法
            } else if (type == 'album') {
              // this.uploadAlbum(); // 同意则调用方法
            }
          }
        })
        // this.uploadCamera(); // 有权限则直接调用方法
      } else {
        // this.uploadCamera()
        this.permissionContent = permissionContent
        this.$refs.permissionModel.open()
        // this.permissionContent = ''
        plus.android.requestPermissions([permission], e => {
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            this.content = content
            this.$refs.permissionModel.close()
            this.$refs.model.open()
          } else if (e.deniedPresent.length > 0) {
            this.$refs.permissionModel.close()
          } else if (e.granted.length > 0) {
            this.$refs.permissionModel.close(); // 别忘了关闭提示
            if (type == 'camera') {
              // this.uploadCamera(); // 同意则调用方法
            } else if (type == 'album') {
              // this.uploadAlbum(); // 同意则调用方法
            }
          }
        })
      }
    },
    uploadImageSuccess(url, res) {
      let that = this
      let c = url
      if (that.conversation.type === "C2C") {
        that.post1('api/im/im/send_friend_msg', {
          friend_id: uni.getStorageSync('friend_id'),
          from_content: c,
          from_type: 'TIMImageElem'
        }, false).then(re => {
          if (re.code == 1) {
            const message = uni.$TUIKit.createImageMessage({
              to: that.getToAccount(),
              conversationType: that.conversation.type,
              payload: {
                file: res
              },
              onProgress: percent => {
                message.percent = percent;
              }
            });
            uni.hideLoading()
            that.$sendTIMMessage(message);
          } else {
            uni.hideLoading()
            uni.showToast({
              title: re.msg,
              icon: "none",
              duration: 1000
            })
          }
        })
      } else if (that.conversation.type === "GROUP") {
        that.post1('api/im/im/send_group_msg', {
          group_id: uni.getStorageSync('group_id'),
          content: c,
          content_type: 'TIMImageElem'
        }, false).then(re => {
          if (re.code == 1) {
            const message = uni.$TUIKit.createImageMessage({
              to: that.getToAccount(),
              conversationType: that.conversation.type,
              payload: {
                file: res
              },
              onProgress: percent => {
                message.percent = percent;
              }
            });

            that.$sendTIMMessage(message);
          } else {
            uni.showToast({
              title: re.msg,
              icon: "none",
              duration: 1000
            })
          }
          uni.hideLoading()
        })
      }
    },
    uploadVideoSuccess(url, res) {
      let that = this
      let c = url
      if (that.conversation.type === "C2C") {
        that.post1('api/im/im/send_friend_msg', {
          friend_id: uni.getStorageSync('friend_id'),
          from_content: c,
          from_type: 'TIMVideoFileElem'
        }, false).then(re => {
          if (re.code == 1) {
            const message = uni.$TUIKit.createVideoMessage({
              to: this.getToAccount(),
              conversationType: this.conversation.type,
              payload: {
                file: res
              },
              onProgress: percent => {
                message.percent = percent;
              }
            });
            uni.hideLoading()
            this.$sendTIMMessage(message);
          } else {
            uni.hideLoading()
            uni.showToast({
              title: re.msg,
              icon: "none",
              duration: 1000
            })
          }
        })
      } else if (that.conversation.type === "GROUP") {
        that.post1('api/im/im/send_group_msg', {
          group_id: uni.getStorageSync('group_id'),
          content: c,
          content_type: 'TIMVideoFileElem'
        }, false).then(re => {
          if (re.code == 1) {
            const message = uni.$TUIKit.createVideoMessage({
              to: this.getToAccount(),
              conversationType: this.conversation.type,
              payload: {
                file: res
              },
              onProgress: percent => {
                message.percent = percent;
              }
            });
            uni.hideLoading()
            this.$sendTIMMessage(message);
          } else {
            uni.hideLoading()
            uni.showToast({
              title: re.msg,
              icon: "none",
              duration: 1000
            })
          }
        })
      }
    },
    handleShootVideo() {
      // #ifdef APP-PLUS
      this.androidCameraPermission('camera', this.content3, this.permissionCameraContent1)
      // #endif
      this.$refs.uploadImg.openAppPermissionAndUploadCameraVideo()
      // this.sendVideoMessage('camera');
    },
    handleSendVideo() {
      // #ifdef APP-PLUS
      this.androidCameraPermission('album', this.content4, this.permissionAlbumContent1)
      // #endif
      this.$refs.uploadImg.openAppPermissionAndUploadAlbumVideo()
    },
    sendVideoMessage(type) {
      let that = this
      uni.chooseVideo({
        sourceType: [type],
        // 来源相册或者拍摄
        maxDuration: 60,
        // 设置最长时间60s
        camera: 'back',
        // 后置摄像头
        success: res => {
          uni.showLoading({
            title: ''
          })
          if (res) {
            uni.uploadFile({
              url: that.BASE_URL + 'addons/cos/index/uploads', //仅为示例，非真实的接口地址
              filePath: res.tempFilePath,
              name: 'file',
              header: {
                token: uni.getStorageSync('token')
              },
              success: (a) => {
                let c = JSON.parse(a.data).data.fullurl
                if (that.conversation.type === "C2C") {
                  that.post1('api/im/im/send_friend_msg', {
                    friend_id: uni.getStorageSync('friend_id'),
                    from_content: c,
                    from_type: 'TIMVideoFileElem'
                  }, false).then(re => {
                    if (re.code == 1) {
                      const message = uni.$TUIKit
                        .createVideoMessage({
                          to: this.getToAccount(),
                          conversationType: this
                            .conversation.type,
                          payload: {
                            file: res
                          },
                          onProgress: percent => {
                            message.percent =
                              percent;
                          }
                        });
                      uni.hideLoading()
                      this.$sendTIMMessage(message);
                    } else {
                      uni.hideLoading()
                      uni.showToast({
                        title: re.msg,
                        icon: "none",
                        duration: 1000
                      })
                    }
                  })
                } else if (that.conversation.type === "GROUP") {
                  that.post1('api/im/im/send_group_msg', {
                    group_id: uni.getStorageSync('group_id'),
                    content: c,
                    content_type: 'TIMVideoFileElem'
                  }, false).then(re => {
                    if (re.code == 1) {
                      const message = uni.$TUIKit
                        .createVideoMessage({
                          to: this.getToAccount(),
                          conversationType: this
                            .conversation.type,
                          payload: {
                            file: res
                          },
                          onProgress: percent => {
                            message.percent =
                              percent;
                          }
                        });
                      uni.hideLoading()
                      this.$sendTIMMessage(message);
                    } else {
                      uni.hideLoading()
                      uni.showToast({
                        title: re.msg,
                        icon: "none",
                        duration: 1000
                      })
                    }
                  })
                }
              }
            })
          }
        }
      });
    },
	}
}